<?php
class wxActionLib
{
	
    /*
    *构建文字回复
    */
    public function send_text($text, $fromUsername, $toUsername)
    {
        $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
                </xml>"; 
        $time = time();
        $msgType = "text";
        $contentStr = $text;
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        return $resultStr;
    }

    /*
    *构建图片回复
    */
    public function send_image($media_id, $fromUsername, $toUsername)
    {
        $imageTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Image><MediaId><![CDATA[%s]]></MediaId></Image>
                </xml>"; 
        $time = time();
        $msgType = "image";
        $mediaId = $media_id;
        $resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $msgType, $mediaId);
        return $resultStr;
    }

    /*
    *构建语音回复
    */
    public function send_voice($media_id, $fromUsername, $toUsername)
    {
        $imageTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Voice><MediaId><![CDATA[%s]]></MediaId></Voice>
                </xml>"; 
        $time = time();
        $msgType = "voice";
        $mediaId = $media_id;
        $resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $msgType, $mediaId);
        return $resultStr;
    }

    /*
    *构建视频回复
    */
    public function send_video($media_id, $title, $description, $fromUsername, $toUsername)
    {
        $imageTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Video>
                <MediaId><![CDATA[%s]]></MediaId>
                <Title><![CDATA[%s]]></Title>
                <Description><![CDATA[%s]]></Description>
                </Video>
                </xml>"; 
        $time = time();
        $msgType = "video";
        $mediaId = $media_id;
        $resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $msgType, $mediaId, $title, $description);
        return $resultStr;
    }

    /*
    *构建图文回复
    */
    public function send_news($news, $fromUsername, $toUsername)
    {
        $newsTplHeader = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <ArticleCount><![CDATA[%d]]></ArticleCount>
                <Articles>"; 
        $newsTplItem = "<item>
                        <Title><![CDATA[%s]]></Title> 
                        <Description><![CDATA[%s]]></Description>
                        <PicUrl><![CDATA[%s]]></PicUrl>
                        <Url><![CDATA[%s]]></Url>
                        </item>";
        $newsTplFooter= "</Articles></xml>";
        $newsCount = $news['count'];
        $resultItem = sprintf($newsTplItem, $news['data']['title'], $news['data']['digest'], $news['data']['file'], $news['data']['link']);
        if($newsCount > 1)
        {
            foreach ($news['data_other'] as $key => $val) {
                $resultItem .= sprintf($newsTplItem, $val['title'], $val['digest'], $val['file'], $val['link']);
            }
        }

        $time = time();
        $msgType = "news";
        $resultStr = "";
        $resultHeader = sprintf($newsTplHeader, $fromUsername, $toUsername, $time, $msgType, $newsCount);
        $resultStr = $resultHeader.$resultItem.$newsTplFooter;
        return $resultStr;
    }
    
}
?>